//
//  MineOrderIMCard.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/10/30.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "MineOrderIMCard.h"
#import "MineTopBottomItem.h"
#import "MineCountModel.h"
@interface MineOrderIMCard ()
@property(nonatomic, strong) UIImageView *bgImageView;
@property(nonatomic, strong) NSMutableArray<MineItemModel *> *itemModels;
@property(nonatomic, strong) NSArray<MineTopBottomItem *> *items;
@end

@implementation MineOrderIMCard
- (void)makeUI {
    [super makeUI];
    self.backgroundColor = UIColor.clearColor;
    [self addSubview:self.bgImageView];
    self.bgImageView.frame = self.bounds;
    
    {
        NSArray *pics = @[ImageName.mine_consumeOrder, ImageName.mine_consumeOrder, ImageName.mine_order, ImageName.mine_im];
        NSArray *titles = @[@"钱包", @"消费订单", @"订单", @"客服"];
        NSArray<NSNumber *> *actions = @[@(ItemActionBill), @(ItemActionConsumeOrder), @(ItemActionOrder), @(ItemActionIM)];
        NSMutableArray<MineItemModel *> *itemModles = [NSMutableArray array];
        CGFloat orinY = 12;
        for (NSInteger index = 0; index < titles.count; index ++) {
            MineItemModel *item = [[MineItemModel alloc]init];
            item.style = ItemStyleOrder;
            item.action = actions[index];
            
            item.picUrl = pics[index];
            item.title = titles[index];
            item.picSize = CGSizeMake(28, 28);
            item.titleFont = kFontMedium(14);
            item.titleColor = rgba(47, 47, 52, 1);
            item.itemMargin = 8;
            [itemModles addObject:item];
        }
        CGFloat itemWidth = ceil(self.width / titles.count);
        for (NSInteger index = 0; index < itemModles.count; index ++) {
            MineTopBottomItem *item = [[MineTopBottomItem alloc]initWithFrame:CGRectMake(itemWidth * index, orinY, itemWidth, self.height * 0.5)];
            [self.bgImageView addSubview:item];
            item.data = itemModles[index];
            // 手势
            item.tag = self.itemModels.count + index;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickItem:)];
            item.userInteractionEnabled = YES;
            [item addGestureRecognizer:tap];
        }
        [self.itemModels addObjectsFromArray:itemModles];
    }
    
    {
        NSArray *titles = @[@"收藏", @"红包/卡券", @"推荐人", @"积分"];
        NSArray<NSNumber *> *actions = @[@(ItemActionCollect), @(ItemActionRedPaper), @(ItemActionTuiJianRen), @(ItemActionJiFen)];
        NSMutableArray<MineItemModel *> *itemModels = [NSMutableArray array];
        NSMutableArray<MineTopBottomItem *> *items = [NSMutableArray array];
        CGFloat orinY = self.height * 0.5 + 18;
        for (NSInteger index = 0; index < titles.count; index ++) {
            MineItemModel *item = [[MineItemModel alloc]init];
            item.style = ItemStyleCollect;
            item.action = actions[index];
            
            item.title = @"0";
            item.subTitle = titles[index];
            item.titleFont = kFontBold(18);
            item.titleColor = rgba(47, 47, 52, 1);
            
            item.subTitleFont = kFontRegular(14);
            item.subtTitleColor = rgba(137, 137, 145, 1);
            
            item.itemMargin = 8;
            
            [itemModels addObject:item];
        }
        CGFloat itemWidth = ceil(self.width / titles.count);
        for (NSInteger index = 0; index < itemModels.count; index ++) {
            MineTopBottomItem *item = [[MineTopBottomItem alloc]initWithFrame:CGRectMake(itemWidth * index, orinY, itemWidth, self.height * 0.5)];
            [self.bgImageView addSubview:item];
            item.data = itemModels[index];
            // 手势
            item.tag = self.itemModels.count + index;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickItem:)];
            item.userInteractionEnabled = YES;
            [item addGestureRecognizer:tap];
            // items
            [items addObject:item];
        }
        [self.itemModels addObjectsFromArray:itemModels];
        self.items = items;
    }
}
- (void)setCountModel:(MineCountModel *)countModel {
    _countModel = countModel;
    for (MineTopBottomItem *item in self.items) {
        if (item.data.action.integerValue == ItemActionCollect) {
            item.data.title = [NSString stringWithFormat:@"%ld", countModel.userCollection];
        } else if (item.data.action.integerValue == ItemActionRedPaper) {
            item.data.title = [NSString stringWithFormat:@"%ld", countModel.userCoupon];
        } else if (item.data.action.integerValue == ItemActionYuYue) {
            item.data.title = [NSString stringWithFormat:@"%ld", countModel.userSubscribe];
        } else if (item.data.action.integerValue == ItemActionZuJi) {
            item.data.title = [NSString stringWithFormat:@"%ld", countModel.userBrowsingFootprints];
        } else if (item.data.action.integerValue == ItemActionTuiJianRen) {
            item.data.title = [NSString stringWithFormat:@"%ld", countModel.inviteesNumber.integerValue];
        } else if (item.data.action.integerValue == ItemActionJiFen) {
            item.data.title = [NSString stringWithFormat:@"%@", [Utilities removeNumberZeroWithDouble:countModel.availableCredits.doubleValue]];
        }
        item.data = item.data;
    }
}
#pragma mark - click
- (void)clickItem:(UITapGestureRecognizer *)ges {
    !self.clickAction ?: self.clickAction(self.itemModels[ges.view.tag]);
}
#pragma mark - lazy
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        UIImageView *iv = [[UIImageView alloc]init];
        iv.image = [UIImage imageNamed:ImageName.mine_bg_block];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.clipsToBounds = YES;
        iv.userInteractionEnabled = YES;
        _bgImageView = iv;
    }
    return _bgImageView;
}
- (NSMutableArray<MineItemModel *> *)itemModels {
    if (!_itemModels) {
        _itemModels = [NSMutableArray array];
    }
    return _itemModels;
}
@end
